<?php
namespace App\Services\Core\Store\Wushi;
use Prophecy\Exception\Exception;

class WushiService
{

    protected $WushiUrl = 'http://card.96rich.com/verify';

    public function WushiSend($bizData)
    {/*{{{*/
        $params = array(
            'chrSet' => 'UTF-8',
            'custId' => WushiConfig::custId,
            'ordId' => date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
            'transType' => 'WS156',
            'busiType' => '',
            'retUrl' => '',
            'timeStamp' => date("YmdHis",time())
        );
//        dd($this->getSignContent($bizData));
        $cusuomer = "{".$this->getSignContent($bizData)."}";
//        dd($cusuomer);
        $params['customerInfo'] = base64_encode($cusuomer);
//        $params['mac_key'] = WushiConfig::privateKey;
        ksort($params);
        $paramsstr = $this ->getSignContent($params);
        $paramsstr = $paramsstr .'&'.'mac_key='.WushiConfig::privateKey;
//        dd($paramsstr);
        $params['signature'] = $this->sign($paramsstr);
        unset($params['mac_key']);
        $resp = $this->_crulPost($params, $this->WushiUrl);
        return $resp;
    }/*}}}*/

    protected function getSignContent($params)
    {/*{{{*/
//        ksort($params);

        $i = 0;
        $stringToBeSigned = "";
        foreach ($params as $k => $v) {
            if ($i == 0) {
                $stringToBeSigned .= "$k" . "=" . "$v";
            } else {
                $stringToBeSigned .= "&" . "$k" . "=" . "$v";
            }
            $i++;
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }/*}}}*/



    protected function sign($data)
    {/*{{{*/
//        header("Content-type: text/html; charset=utf-8");
//        $data =;
        $sign = strtoupper(md5($data));
        return $sign;
    }/*}}}*/


    public function validate($param)
    {
        ksort($param);
//        $param['respMsg'] = utf8_encode($param['respMsg']);
//        $param['respMsg'] = mb_convert_encoding($param['respMsg'], "UTF-8", "auto");
        $paramstr = $this->getSignContent($param);
        $paramstr = $paramstr .'&'.'mac_key='.WushiConfig::privateKey;
        $sign = $this->sign($paramstr);
        return $sign;
    }



    private function _crulPost($postData, $url = '')
    {/*{{{*/
        if (empty($url)) {
            //Yii::log('openapi curl post数据时，目标url为空','error');
            return false;
        }

        try {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postData));
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            $res = curl_exec($curl);

            $errno = curl_errno($curl);
            $curlInfo = curl_getinfo($curl);
            $errInfo = 'curl_err_detail: ' . curl_error($curl);
            $errInfo .= ' curlInfo:' . json_encode($curlInfo);

            $arrRet = json_decode($res, true);

            //统一记录日志
            $logLevel = 'info';
            if (!is_array($arrRet)) {
                $logLevel = 'warning';
            }
            curl_close($curl);
        } catch (Exception $e) {
            print_r($e->getMessage());
        }

        //Yii::log("openapi curl post url: \t $url \t post: \t " . json_encode($postData) . " \t errno: $errno return: $res " . $errInfo, $logLevel);


        return $arrRet;
    }/*}}}*/
}
